#!/usr/bin/python
# -*- coding: UTF-8 -*-

__metaclass__ = type
class FooBar:
	def __init__(self, value=42):
		self.somevar = value
f = FooBar()
print f.somevar
print '------------------------------'

class Bird:
	def __init__(self):
		self.hungry = True

	def eat(self):
		if self.hungry:
			print 'Aaaah...'
			self.hungry = False
		else:
			print 'No, thanks!'

b = Bird()
b.eat()
b.eat()

class SongBird(Bird):
	def __init__(self):
		# 旧式类绑定方法
		# Bird.__init__(self)
		
		# 新式类方法
		super(SongBird, self).__init__() 
		self.sound = 'Squawk!'
	
	def sing(self):
		print self.sound
sb = SongBird()
sb.sing()
sb.eat()

# 检查参数 是否是字符串 或 者key
def checkIndex(key):
	if not isinstance(key, (int, long)):raise TypeError
	if key < 0:raise IndexError

# 成员访问, 创建一个类似于字典的对象
class ArithmeticSequence:
	def __init__(self, start=0, step=1):
		self.start = start
		self.step = step
		self.changed = {}

	def __getitem__(self, key):
		checkIndex(key)
		try:
			return self.changed[key]
		except KeyError:
			return self.start+key*self.step

	def __setitem__(self, key, value):
		checkIndex(key)
		self.changed[key] = value


s = ArithmeticSequence(1,2)
print s[4]

s[4] = 2
print s[4]
print s[5]
print '------------------------------'

# 子类化列表, 字典和字符串
class CountList(list):
	def __init__(self, *args):
		super(CountList, self).__init__(*args)
		self.count = 0
	def __getitem__(self, index):
		self.count += 1
		return super(CountList, self).__getitem__(index)


c = CountList(range(10))
print c

c.reverse()
print c
print c.count
print c[1] + c[3]
print c.count
print '---------------属性---------------'

class Rectangle:
	def __init__(self):
		self.width = 0
		self.height = 0

	def setSize(self, size):
		self.width, self.height = size

	def getSize(self):
		return self.width, self.height

	size = property(getSize, setSize)

r = Rectangle()
r.width = 10
r.height = 20

# 相当于 r.getSize()
print r.size

# 相当于 r.setSize()
r.size = (100, 200)
print r.width, r.height

print '---------------静态方法和类成员方法---------------'
# 手动包装静态方法和成员方法
class MyClass:
	def smeth():
		print 'This is a static method'
	smeth = staticmethod(smeth)

	def cmeth(cls):
		print "This is a class method of", cls
	cmeth = classmethod(cmeth)

MyClass.smeth()
MyClass.cmeth()

# 装饰器修饰
class YouClass:
	@staticmethod
	def smeth():
	        print 'This is a 装饰器修 static method'
	
	@classmethod	          
	def cmeth(cls):
	        print "This is a 装饰器修 class method of", cls
YouClass.smeth()
YouClass.cmeth()


print '---------------__getattr__, __setattr__ 方法---------------'



print '---------------迭代器---------------'
class Fibs:
	def __init__(self):
		self.a = 0
		self.b = 1

	def next(self):
		self.a, self.b = self.b, self.a+self.b
		return self.a

	def __iter__(self):
		return self

fibs = Fibs()
for f in fibs:
	print f
	if f > 100:break

class testIterator:
	value = 0
	def next(self):
		self.value += 1
		if self.value > 10: raise StopIteration
		return self.value
	def __iter__(self):
		return self

ti = testIterator()
print list(ti)

print '---------------生成器---------------'
def flatten(nested):
	for sublist in nested:
		for element in sublist:
			yield element

nested = [[1,2], [3,4], [5]]
for num in flatten(nested):
	print num

print list(flatten(nested))

slist = [i for i in flatten(nested)]
print slist

sublist = [i for i in flatten(nested) if i % 2 == 0]
print sublist

print '---------------生成器---------------'







